package cn.aitstone.org.controller;

import cn.aitstone.basic.annotation.PreAuthorize;
import cn.aitstone.basic.utils.AjaxResult;
import cn.aitstone.basic.utils.LoginContext;
import cn.aitstone.basic.utils.PageList;
import cn.aitstone.org.domain.Department;
import cn.aitstone.org.query.DepartmentQuery;
import cn.aitstone.org.service.IDepartmentService;
import cn.aitstone.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 * @Description: 部门接口类
 * @Author: Sheldon
 * @Version: 1.0
 * @Date:  2022/6/24 11:07
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
    * @Title: loadById
    * @Description: 根据部门id查询
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/24 11:08
    * @Parameters: [id]
    * @Return cn.aitstone.org.domain.Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department loadById(@ApiParam(value="部门表的主键",required = true) @PathVariable("id") Long id){
        return departmentService.loadById(id);

    }

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/24 11:08
    * @Parameters: []
    * @Return java.util.List<cn.aitstone.org.domain.Department>
    */
    @GetMapping
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }

    /**
    * @Title: remove
    * @Description: 根据id删除
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/24 11:47
    * @Parameters: [id]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据id删除部门", sn="department:delete")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }

    }

    /**
    * @Title: addOrUpt
    * @Description: 新增和修改接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/24 11:47
    * @Parameters: [department]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PutMapping
    @PreAuthorize(name = "新增和修改部门", sn="department:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.save(department);
            } else{
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }

    }

    /**
    * @Title: queryData
    * @Description: 高级查询分页接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/24 11:48
    * @Parameters: [departmentQuery]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping
    @PreAuthorize(name = "获取部门列表", sn="department:list")
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery, HttpServletRequest req){
        Logininfo loginInfo = LoginContext.getLoginInfo(req);
        System.out.println(loginInfo);
        PageList<Department> data = departmentService.queryData(departmentQuery);
        return data;

    }

    /**
    * @Title: batch
    * @Description: 批量删除
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/25 11:18
    * @Parameters:
    * @Return
    */
    @PatchMapping
    @PreAuthorize(name = "批量删除部门", sn="department:batch")
    public AjaxResult batch(@RequestBody Long[] ids){
        try {
            departmentService.batchdel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }

    }

    /**
    * @Title: tree
    * @Description: 获取部门树数据接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/25 20:50
    * @Parameters: []
    * @Return java.util.List<cn.aitstone.org.domain.Department>
    */
    @GetMapping("/tree")
    public List<Department> tree(){
        return departmentService.tree();
    }

}
